import my_function as my
import re

my_dict = ({'state_infrastructure_add': '100',
            'state_market_access_price_impact': '2', 
            'country_max_weekly_construction_progress_add': '1000', 
            'state_building_port_max_level_add': '100', 
            'country_bureaucracy_add': '1000', 
            'country_weekly_innovation_add': '100', 
            'country_weekly_innovation_max_add': '5000', 
            })

# path
game_path = '/home/h/games/v3'
modifiers_path = '/game/common/static_modifiers/00_code_static_modifiers.txt'
file_path = game_path + modifiers_path
# check if base_value
base_value_check = 0
# read file
with open(file_path, 'r', encoding='utf-8') as file:
    content = file.readlines()
new_content = []

# replace
for line in content:
    if line.strip() == 'base_values = {' :
        base_value_check += 1
    if base_value_check > 0 and '}' in line :
        base_value_check -= 1
    if base_value_check > 0:
        for key in my_dict:
            regular_express = key + ' ' + '=' + ' ' + '.*'
            target = key + ' ' + '=' + ' ' + my_dict[key]
            line = re.sub(regular_express, target, line)
    new_content.append(line)
# add
# base_value_check = 0
new_content2 = []
modifiers_to_add = ['state_birth_rate_mult = 0.5','state_building_port_max_level_add = 1000','state_building_construction_sector_max_level_add = 1000', 'state_building_barracks_max_level_add = 1000']
for line in new_content:
    if line.strip() == 'base_values = {' :
        new_content2.append(line)
        for i in modifiers_to_add:
            new_content2.append('\t' + i + '\n')
        continue
    new_content2.append(line)


# for key in my_dict:
#     regular_express = key + ' ' + '=' + ' ' + '.*'
#     target = key + ' ' + '=' + ' ' + my_dict[key]
#     # print(regular_express, target)
#     for line in content:
#         if line.strip() == 'base_values = {' :
#             base_value_check += 1
#         if base_value_check > 0 and '}' in line :
#             base_value_check -= 1
#         if base_value_check > 0:
#             line = re.sub(regular_express, target, line)
#         new_content.append(line)

with open(file_path, 'w', encoding='utf-8') as file:
    file.writelines(new_content2)